package com.cmccgz.udacs_landlord_webapp.main.warning.view;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.WarningDetailAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.WarningDetail;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.main.warning.WarningActivity;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.WarningDetailContract;
import com.cmccgz.udacs_landlord_webapp.main.warning.presenters.WarningDetailPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.PopWindowUtils;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.cmccgz.udacs_landlord_webapp.utils.YRecyclerView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报障详情
 * @author INX
 */
public class WarningDetailFragment extends BaseFragment implements WarningDetailContract.View, XRecyclerView.LoadingListener,View.OnClickListener, OnRvItemClickListenter<Map<String,Object>> {
    private String w_id;
    private WarningDetailPresenter presenter;
    private YRecyclerView rv_warning;
    private WarningActivity parentActivity;
    private WarningDetailAdapter adapter;
    private static final String KEY_MENU_TOKEN="key_menu_token";
    private static final int TOKEN_DEAL=0x1;
    private static final int TOKEN_EDIT=0x2;
    private static final int TOKEN_DETE=0x3;
    private PopupWindow mPopupWindow;
    private ViewHelper viewHelper;
    private LinearLayout ll_op;
    private TextView tv_confirm;
    private TextView tv_reject;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_warning_detail;
    }

    @Override
    protected void initView() {
        viewHelper = ViewHelper.instance(this);
        viewHelper.setTitle("报障详情");
        viewHelper.setAllVisable(false);
        viewHelper.setBackTextVisable(true);

        viewHelper.setImageRight(R.mipmap.menu);

        parentActivity = ((WarningActivity) getActivity());
        presenter = new WarningDetailPresenter(this);
        rv_warning = ((YRecyclerView) findViewById(R.id.rv_warning));
        adapter = new WarningDetailAdapter(parentActivity,this);
        rv_warning.setLayoutManager(new LinearLayoutManager(parentActivity,LinearLayoutManager.VERTICAL,false));
        rv_warning.addItemDecoration(new MyDividerItemDecoration(parentActivity, DividerItemDecoration.VERTICAL));
        rv_warning.setAdapter(adapter);
        rv_warning.setLoadingListener(this);
        rv_warning.setLoadingMoreEnabled(false);

        ll_op = ((LinearLayout) findViewById(R.id.ll_op));
        tv_confirm = ((TextView) findViewById(R.id.tv_confirm));
        tv_reject = ((TextView) findViewById(R.id.tv_reject));
        tv_confirm.setOnClickListener(this);
        tv_reject.setOnClickListener(this);
        initDatas();
    }

    private void initDatas() {
        Bundle arguments = getArguments();
        w_id = arguments==null?"":arguments.getString("w_id");
        if (StringUtils.isEmpty(w_id)) {
            showTipsDialog("提示", "当前传入的的参数异常，请重新操作", "确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    tipsDialog.dismiss();
                }
            });
            tipsDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mContext.onBackPressed();
                }
            });
        }else{
            getDetail(true);
        }
    }

    private void getDetail(boolean isShowLoading) {
        if (isShowLoading) {
            showLoadingDialog("正在加载障单详情...");
        }
        presenter.getDetail(w_id);
    }

    @Override
    public void getDetailSuccess(WarningDetail detail) {
        adapter.setWarningDetail(detail);
        dismissLoadingDialog();
        rv_warning.refreshComplete();

        viewHelper.setImageRightVisable(detail!=null//详情为空
                &&("1".equals(detail.w_state) &&!parentActivity.isAttendant()//非维护人员并是未派单
                ||"2".equals(detail.w_state)&&parentActivity.isAttendant()));//维护人员并是处理中

        //显隐操作栏
        if (detail!=null&&"3".equals(detail.w_state)&&!parentActivity.isAttendant())
            ll_op.setVisibility(View.VISIBLE);
        else
            ll_op.setVisibility(View.GONE);
    }

    @Override
    public void getDetailError(String msg) {
        dismissLoadingDialog();
        rv_warning.refreshComplete();
        showToast(msg);
        viewHelper.setImageRightVisable(false);
        ll_op.setVisibility(View.GONE);
    }

    @Override
    public void deleSuccess() {
        dismissLoadingDialog();
        ToastUtils.show(mContext,"删除成功！");
        parentActivity.onBackPressed();
        parentActivity.refreshWarningList();
    }

    @Override
    public void deleError(String msg) {
        dismissLoadingDialog();
        ToastUtils.show(mContext,msg);
    }

    @Override
    public void confirmSuccess() {
        dismissLoadingDialog();
        ToastUtils.show(mContext,"确认成功！");
        getDetail(true);
        parentActivity.refreshWarningList();
    }

    @Override
    public void confirmError(String msg) {
        dismissLoadingDialog();
        ToastUtils.show(mContext,msg);
    }

    @Override
    public void onRefresh() {
        getDetail(false);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                parentActivity.onBackPressed();
                break;
            case R.id.iv_right:
                if (mPopupWindow==null) {
                    List<Map<String, Object>> datas=new ArrayList<>();
                    HashMap<String, Object> menuItem_1 = new HashMap<>();
                    HashMap<String, Object> menuItem_2= new HashMap<>();
                    if (parentActivity.isAttendant()) {
                        menuItem_1.put(PopWindowUtils.KEY_MENU_NAME,"处理");
                        menuItem_1.put(KEY_MENU_TOKEN, TOKEN_DEAL);
                        datas.add(menuItem_1);
                    }else{
                        menuItem_1.put(PopWindowUtils.KEY_MENU_NAME,"编辑");
                        menuItem_1.put(KEY_MENU_TOKEN, TOKEN_EDIT);
                        menuItem_2.put(PopWindowUtils.KEY_MENU_NAME,"删除");
                        menuItem_2.put(KEY_MENU_TOKEN, TOKEN_DETE);
                        datas.add(menuItem_1);
                        datas.add(menuItem_2);
                    }
                    mPopupWindow = PopWindowUtils.getMenuListPW(mContext,datas,this);
                }
                if (!mPopupWindow.isShowing()) {
                    mPopupWindow.showAsDropDown(v);
                }
                break;
            case R.id.tv_reject://否决
                parentActivity.editWarning(WarningEditFragment.MODE_REJECT,adapter.getDetail());
                break;
            case R.id.tv_confirm://确认
                showLoadingDialog("正在提交");
                presenter.confirmWarning(w_id);
                break;
        }
    }

    @Override
    public void onItemClick(Map<String, Object> stringObjectMap, int position, View itemView) {
        dismissMenu();
        int token = (Integer) stringObjectMap.get(KEY_MENU_TOKEN);
        switch (token) {
            case TOKEN_DEAL://处理
                parentActivity.dealWarning(adapter.getDetail());
                break;
            case TOKEN_EDIT://编辑
                parentActivity.editWarning(WarningEditFragment.MODE_EDIT,adapter.getDetail());
                break;
            case TOKEN_DETE://删除
                showLoadingDialog("正在删除");
                presenter.deleWarning(adapter.getDetail().w_id);
                break;
        }
    }

    public boolean isMenuShowing(){
        return mPopupWindow!=null&&mPopupWindow.isShowing();
    }
    public void showMenu(){
        viewHelper.getIv_right().performClick();
    }
    public void dismissMenu(){
        if (mPopupWindow!=null&&mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }
    public void refreshDetail(){
        getDetail(true);
    }
}
